float2 HalfPixel;

texture Depth;
sampler depthSampler = sampler_state
{
    Texture = <Depth>;
    AddressU = Clamp;
    AddressV = Clamp;
    MagFilter = Point;
    MinFilter = Point;
    Mipfilter = Point;
};

struct ShaderData
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

ShaderData VertexShaderFunction(ShaderData input)
{
    return input;
}

float SobelFilter(sampler texSampler, float2 texCoord)
{
    float tl = tex2D(texSampler, texCoord + HalfPixel * 2 * float2(-1, -1));
    float t  = tex2D(texSampler, texCoord + HalfPixel * 2 * float2( 0, -1));
    float tr = tex2D(texSampler, texCoord + HalfPixel * 2 * float2( 1, -1));
    float r  = tex2D(texSampler, texCoord + HalfPixel * 2 * float2( 1,  0));
    float br = tex2D(texSampler, texCoord + HalfPixel * 2 * float2( 1,  1));
    float b  = tex2D(texSampler, texCoord + HalfPixel * 2 * float2( 0,  1));
    float bl = tex2D(texSampler, texCoord + HalfPixel * 2 * float2(-1,  1));
    float l  = tex2D(texSampler, texCoord + HalfPixel * 2 * float2(-1,  0));

    float dx = abs(tr + 2 * r + br - tl - 2 * l - bl);
    float dy = abs(bl + 2 * b + br - tl - 2 * t - tr);

    return dx + dy;
}

float4 PixelShaderFunction(float2 screenCoord : TEXCOORD0) : COLOR0
{
    return SobelFilter(depthSampler, screenCoord) * 32;
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
